﻿
$ObjectFactory=function(){this.initialize.apply(this,arguments);}
$ObjectFactory.prototype={
		
	initialize : function(){
		this.prefix="biz.zheng.";
	},
		
	getPrefix :	function(){
		return this.prefix;
	},
	
	/* render name */
	renderName : function(iName){
		if((typeof(iName)!="string")||(!iName)||(parseInt(iName)))return false;
		var QName = iName.replace(/ /g,"");
		if( QName.indexOf("$")==0 )   QName=this.getPrefix()+QName.substr(1);
		if( QName.indexOf("ll.")==0 ) QName=this.getPrefix()+QName.substr(3);
		if( QName.indexOf("bz.")==0 ) QName=this.getPrefix()+QName.substr(3);
		
		var sEnd=QName.charAt(QName.length-1);
	    if(sEnd=="*"){
	    	QName=QName.substr(0,QName.length-1)+"_base";
	    }

		return QName;
	},

	/*to denine a varous as fn [or {} if fn==null] */
	define : function(iName,fn){
		var QName = this.renderName(iName);
		if(!QName)return false;
		return fn?this._defineFunc(QName,fn):this._defineName(QName)
	},
	
	/*	inner; to define a various as {}*/
	_defineName : function(QName){
		var pNames=QName.split(".");
		var pkgLength=0;
		var namedObj;
		for(var i=0;i<pNames.length;i++){
			pkgLength += (pNames[i].length+1);
			var pkgName = QName.substr(0,pkgLength-1);
			var pkgType = eval("typeof "+pkgName);
			if("undefined"==pkgType){
				//TODO: how about: object!=pkgType;
				namedObj = new Object();
				eval(pkgName+" = namedObj;");
			}else{
				eval("namedObj = pkgName;");
			}
		}
		return namedObj;
	},
	
	/* inner; to define a various as fn */
	_defineFunc : function (QName,fn){
		try{
			eval( " _____ = " + QName ); 
			if(_____) return  _____; //if no error,means been named,return old;
		}catch(e){}
		try{
			eval( " _____ = " + fn );
			this._defineName(QName);
			eval( QName + " = _____ " );
			return  _____ ;
		}catch(e){
			//alert("defineFunc for "+QName+":\n "+e.description)
		}
		return false;
	}
};

$objectFactory = new $ObjectFactory();
function $NameSpace(QName){return $objectFactory.define(QName);}
function $GlobalDefine(QName,fn){return $objectFactory.define(QName,fn);}
$GlobalDefine("$ObjectFactory","$ObjectFactory");
$GlobalDefine("$objectFactory","$objectFactory");

function NameSpace(QName){return $NameSpace(QName);}//@dpreciated;
function GlobalDefine(QName,fn){return $GlobalDefine(QName,fn);}//@dpreciated;
function packageNamed(QName){return $objectFactory.define(QName);}//@dpreciated;
function ClassNamed(QName){return $objectFactory.define(QName);}//@dpreciated;
function ObjectNamed(QName,fn){return $objectFactory.define(QName,fn);}//@dpreciated;
function globalNamed(QName,fn){return $objectFactory.define(QName,fn);}//@dpreciated;





function between(param,container){
	return  belongtoContainer(param,container);
}

function belongto(param,container){
	return  belongtoContainer(param,container);
}

//param need to be judged whether it belongs to container;
function belongtoContainer(param,container){
	var aObj = param;
	var cObj = container;
	if(!cObj)return false;//null,false,"",undifined;
	if(aObj==cObj)return true;
	
	var	isContained = false;
	switch(typeof(cObj)){
	 //case "undefined" : break;
	 case "boolean" :
		isContained = cObj;
	 break;
	 case "number" :
		isContained = (parseNumber(aObj)==cObj);
	 break;
	 case "string" :
			// need to extra processing.
			isContained = belongtoCollection(param,container);
	 break;
	 case "object" :
		if (cObj instanceof Array){
			isContained = belongtoArray(aObj,cObj);
		}else{
			isContained = belongtoObject(aObj,cObj);
		}
	 break;
	 case "function" :
		isContained = cObj.call(null,aObj);
		if(typeof(isContained)!="boolean"){
			isContained=false;
		}
	 break;
	 default :
	}
	return isContained;
	
}

function belongtoArray(param,objects){
	var aObj = param;
	var cObj = objects;
	var aNumber = parseNumber(aObj);
	for(var i = 0; i < cObj.length ; i++){
		if(	cObj[i]== aObj || parseNumber(cObj[i])==aNumber){
			return true;
		}
	}
	return false;
}

function belongtoObject(param,objects){
	var aObj = param;
	var cObj = objects;
	var aNumber = parseNumber(aObj);
	for(var key in cObj){
		if(	cObj[key]== aObj || parseNumber(cObj[key])==aNumber){
			return true;
		}
	}
	return false;
}

function belongtoCollection(param,aCollection){
	//if(typeof(aIterator)=="boolean")return aIterator;
	//section like "{-∞:2,4:6,∞}"; "{5,6;8}";"{(5.8,8]}";
	var tList= aCollection.replace(/\s/g,"");
	var minTokenIndex = tList.lastIndexOf("{");
	var maxTokenIndex = tList.indexOf("}");
	if( minTokenIndex<0 || minTokenIndex>=maxTokenIndex	){
		// if no collectiontoken, processing as a section;
		return belongtoSection(param,tList);
	}
	
	tList = tList.replace(",",";").substring(minTokenIndex+1,maxTokenIndex);
	var sections = tList.split(";");
	var matched	= false;
	for(var i=0;i<sections.length;i++){
		if(sections[i].indexOf(":")>-1){
			var newSection="["+sections[i].replace(":",",")+"]";
			if( belongtoSection(param,newSection) ){
				matched = true;
				break;
			}
		}else if( sections[i]== param ||
				  parseNumber(sections[i])==parseNumber(param)
		){
			matched = true;
			break;
		}
		
	}
	return matched;
}

/**
alert(between(0,"{0,2:+∞}" ));//true;
alert(between( any object,false)); //false;
alert(between( 1,true )); //true;
alert(between( 1,"true" )); //false;
var aaa={}; aaa.a=56;
alert(between(56,aaa )); //true;
var aab=[]; aab[1]=56;
alert(between(56,aab )); //true;
**/


//deep id recuit deep avoid no-end-loop
function objectToString(cObj,deep,pkey){
	//typeof(null)="object", should add an option for whether need show null / undefined;
	if(cObj==null)return "null";
	var toStr="",maxDeep=5;
	var myDeep = parseInt(deep);
	myDeep = (isNaN(myDeep)||myDeep>5)?5:myDeep;
	if( myDeep<=0 ) return toStr;//arrived maxDeep!
	
	switch(typeof(cObj)){
	 case "undefined" : ;
	 	toStr+="undefined"; 
	 	break;
	 case "function" :
	 	var func;
	 	func=cObj.toString();//show function source;
	 	func=func.substr(0,func.indexOf("{")); //only show function name;
	 	//func="({function})"; //only indicate it is a function;
	 	toStr+=func; 
	 	break;
	 case "object" :
	 	if(cObj instanceof Object){
	 		var	aValue;
		 	for( var key in cObj ){
		 		var qkey = pkey?(pkey+"."+key):key;
		 		// for debug: if(key=="xmlNode")aValue="({XMLNODE})";else 
		 		aValue = objectToString(cObj[key],myDeep-1,qkey);
		 		//if(aValue) toStr += ( key+"="+aValue+"; " );
		 		if(aValue) toStr += ( "<br/>\n"+qkey+" = "+aValue+" ;" );
		 	}
	 	}else{
	 		//toStr += "NOT JS OBJECT";//like xml Node;
	 	}
		if(toStr)toStr="["+toStr+"]";
	 	break;
	 default ://boolean,number,string;
	 	toStr += cObj; //toStr= "" + cObj;
	}
	return toStr;
}

_objectMap=new Array();
function getObject(key){
	var objKey = key;
	if(!key){
		do{ objKey = "O"+Math.floor(10000*Math.random());//caps O;
		}while(_objectMap[objKey]);
	}
	if(!_objectMap[objKey])_objectMap[objKey]=new Array();
		_objectMap[objKey].key = objKey;
	return _objectMap[objKey];
}

function genObject(genHandle){
	var obj = null;
	switch(typeof genHandle){
		case "string" :
			//try{obj = eval(genHandle);}catch(e){}//error should throw;
			obj = eval(genHandle);
			if(typeof(obj)=="function")obj=obj();
			break;
		case "function" :
			obj = genHandle();
			break;
		default:
			obj = genHandle;
	}
	return obj;
}

function getCachedObject(key,fnHandle,oLocked){
	var oCached = getObject(key);
	if(!(oCached.cached && oCached.object) ){
		oCached.cached = true;
		oCached.object = genObject(fnHandle);
	}else if(oCached.locked){
		oCached.cached = true;
		oCached.object = genObject(fnHandle);
	}
		oCached.locked = oLocked; 
	return oCached;
}

function releaseCachedObject(key,oLocked){
	var oCached = getObject(key);
	if(	oCached.locked && (oCached.locked==oLocked)){
		oCached.locked = false;
	}
}

function getObjHandle(arrObjLists){
	   var objList = arrObjLists;
	   var returnObj = null;
	   for (var i=0; i<objList.length; i++) {
			 try {
			    returnObj = new ActiveXObject(objList[i]);
	   			//this.logger.info("succeed in getting obj: "+objList[i]);
				break;
			 }catch (e){
				if(i==(objList.length-1));//this.logger.error("Failure in getting obj from list"+arrObjLists);
			 }
	   }
	   return returnObj;
}


function getDomHandle(isAsync){
   var objList = ['MSXML3.DOMDocument','Msxml2.DOMDocument.4.0', 'Msxml2.DOMDocument', 'MSXML.DOMDocument', 'Microsoft.XMLDOM','Microsoft.DOMDocument'];
   var objReturn  = getObjHandle(objList);
   if(objReturn) objReturn.async = isAsync?true:false;
   return objReturn;
}

function getHttpHandle() {
  //var objList = ['Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
  var objReturn;
  //* IE7 also XMLHttpRequest;
  if(window&&window.XMLHttpRequest){// Mozilla, Safari,..
       objReturn = new XMLHttpRequest();
       /*
       if (objReturn.overrideMimeType) {
           objReturn.overrideMimeType('text/xml');
       }
       * */
  }else{
  		var objList = ['Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'];
  		objReturn = getObjHandle(objList);
  }
  return objReturn;
}

//from prototype, can't be replaced, put here because  $import>doHttpPost>fnBind>$A
function $A(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}

function fnBind() {
  var args = $A(arguments), method = args.shift(), object = args.shift();
  var retval = function() {
    return method.apply(object, args.concat($A(arguments)));
  }
  retval.method = method;
  return retval;
}

function objectEval(obj,str){
	var method = function(){
		var innerReturn = eval(str);
		if(typeof(innerReturn)=="function"){
			innerReturn = innerReturn.apply(obj);
		}
		/*
		else if(typeof(objReturn)=="undefined"){
			var newStr = "this."+str; //if forget "this."
			innerReturn = eval(newStr);
			if(typeof(innerReturn)=="function"){
				innerReturn = innerReturn.apply(obj);
			}
		}
		* */
		return innerReturn;
	} 
	return method.apply(obj);
}
 
function doHandle(handle1,args){
	if(typeof(handle1)=="function"){
		handle1(args);
	}else if(typeof(handle1)=="string"){
		var handle2 ;
		try{handle2 = eval(handle1);}catch(e){}
		if(typeof(handle2)=="function"){
			handle2(args);
		}
	}
}

